package com.bw.common.mvvm.viewmodel;

import android.os.Bundle;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.bw.common.entity.NetErroEntity;
import com.bw.common.field.BusAction;
import com.bw.common.field.DataActionEntity;
import com.bw.common.mvvm.model.BaseRespository;
import com.bw.common.utils.ToastShowUtils;
import com.bw.network_sdk.BaseEntity;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends BaseRespository> extends ViewModel
        implements IViewModel, Observer<BaseEntity> {

    private CompositeDisposable compositeDisposable;
    protected M respository;//负责全部网络请求
    private UIDataFunction uiDataFunction;

    public UIDataFunction getUiDataFunction() {
        return uiDataFunction;
    }

    public BaseViewModel(){
        uiDataFunction = new UIDataFunction();
        compositeDisposable = new CompositeDisposable();
        initRespository();
    }

    private void initRespository(){
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) type.getActualTypeArguments()[0];
        try {
            respository = (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public abstract void onResult(BaseEntity entity);

    protected abstract void initData();
    @Override
    public void onCreate() {
        initData();
    }
    @Override
    public void onDestroy() {
        //统一解除rxjava订阅
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }
    //显示dialog
    protected void showDialog(){
        DataActionEntity entity = new DataActionEntity();
        entity.action = BusAction.SHOWDIALOG;
        uiDataFunction.post(entity);
    }
    //关闭dialog
    protected void hideDialog(){
        DataActionEntity entity = new DataActionEntity();
        entity.action = BusAction.HIDEDIALOG;
        uiDataFunction.post(entity);
    }
    //开启界面
    protected void startPage(String path){
        DataActionEntity entity = new DataActionEntity();
        entity.action = BusAction.STARTPAGE;
        HashMap<String,Object>map=new HashMap<>();
        map.put("activity",path);
        entity.data = map;
        uiDataFunction.post(entity);
    }
    //传参开启界面
    protected void startBundPage(String path, Bundle bundle){
        DataActionEntity entity = new DataActionEntity();
        entity.action = BusAction.STARTBUNDPAGE;
        HashMap<String,Object>map=new HashMap<>();
        map.put("activity",path);
        map.put("bundle",bundle);
        entity.data = map;
        uiDataFunction.post(entity);
    }
    //带返回值开启界面
    protected void startResultPage(String path){
        DataActionEntity entity = new DataActionEntity();
        entity.action = BusAction.STARTRESULTPAGE;
        HashMap<String,Object>map=new HashMap<>();
        map.put("activity",path);
        entity.data = map;
        uiDataFunction.post(entity);
    }
    //关闭界面
    public void finishPage(){
        DataActionEntity entity = new DataActionEntity();
        entity.action = BusAction.FINISHPAGE;
        uiDataFunction.post(entity);
    }
    @Override
    public void onSubscribe(@NonNull Disposable d) {
        //统一保存订阅记录
        compositeDisposable.add(d);
        showDialog();
    }
    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        //根据返回值BaseEntity 进行类型判断->是否为具体出错类型
        if (baseEntity instanceof NetErroEntity){
            NetErroEntity netErroEntity = (NetErroEntity) baseEntity;
            ToastShowUtils.showShort(netErroEntity.message);
            return;
        }
        onResult(baseEntity);
    }
    @Override
    public void onError(@NonNull Throwable e) {
        LogUtils.e("onError:"+e.getMessage());
        ToastShowUtils.showShort("请求错误");
    }
    @Override
    public void onComplete() {
        hideDialog();
    }

    public static class UIDataFunction{
        private MutableLiveData<DataActionEntity>uiData=new MutableLiveData<>();

        public MutableLiveData<DataActionEntity> getUiData() {
            return uiData;
        }

        public void post(DataActionEntity entity){
//            LogUtils.e("UIDataFunction:"+entity.action);
            uiData.setValue(entity);
        }
    }

}
